/*
 * 
 */ 

package jackal;

import javax.swing.*;
import javax.swing.border.*;
import javax.accessibility.*;

import java.awt.*;
import java.awt.event.*;

import java.util.*;

/* 
 * 
 */
public class GuiJFrame extends JFrame {
    public JLayeredPane layeredPane;
    
    /**
     * 
     * @param comp
     * @param index 
     */
    public void addComponentOnIsland(Component comp, Object index) {
        layeredPane.add(comp, index);
    }
    
    public void initGamers(Gamer gamer){
        this.addComponentOnIsland(gamer.boat, new Integer(2));
        this.addComponentOnIsland(gamer.pirate1, new Integer(3));
        this.addComponentOnIsland(gamer.pirate2, new Integer(3));
    }
    
    public void deleteComponentFromIsland(Component comp) {
        for (int j=0; j<layeredPane.getComponentCount(); j++) {
            if (layeredPane.getComponent(j).hashCode() == comp.hashCode()) {
                layeredPane.remove(j);
            }
        }
    }
    
    public void setNullBorder() {
        for (Component c : layeredPane.getComponents()) {
            if (c instanceof JLabel) {
                ((JLabel) c).setBorder(null);
            }
        }
    }
    
    public void displayAllObjects(World world) {
        for (int i=0; i<Island.BOARD_SIZE; i++) {
            for (int j=0; j<Island.BOARD_SIZE; j++) {
                world.island.getPlace(i, j).setLocation(i*Place.CELL_SIZE, j*Place.CELL_SIZE);
            }
        }
       
        world.gamer1.boat.setLocation(world.gamer1.boat.getCoord().x*Place.CELL_SIZE, 
                world.gamer1.boat.getCoord().y*Place.CELL_SIZE);  
        world.gamer1.pirate1.setLocation(world.gamer1.pirate1.getCoord().x*Place.CELL_SIZE + world.gamer1.pirate1.offsetX ,
                world.gamer1.pirate1.getCoord().y*Place.CELL_SIZE);
        world.gamer1.pirate2.setLocation(world.gamer1.pirate2.getCoord().x*Place.CELL_SIZE + world.gamer1.pirate2.offsetX,
                world.gamer1.pirate2.getCoord().y*Place.CELL_SIZE);

        world.gamer2.boat.setLocation(world.gamer2.boat.getCoord().x*Place.CELL_SIZE, 
                world.gamer2.boat.getCoord().y*Place.CELL_SIZE);
        world.gamer2.pirate1.setLocation(world.gamer2.pirate1.getCoord().x*Place.CELL_SIZE + world.gamer2.pirate1.offsetX,
                world.gamer2.pirate1.getCoord().y*Place.CELL_SIZE);
        world.gamer2.pirate2.setLocation(world.gamer2.pirate2.getCoord().x*Place.CELL_SIZE + world.gamer2.pirate2.offsetX,
                world.gamer2.pirate2.getCoord().y*Place.CELL_SIZE);

        for (int i=0; i<world.getMoney().size(); i++) {
            Money money = world.getMoney().get(i);
            if (money.showedState == Money.STATE_CREATED) {
                this.addComponentOnIsland(money, new Integer(2));
                money.showedState = Money.STATE_VISIBLE;
            }
            
            money.setLocation(money.getCoord().x*Place.CELL_SIZE + money.offset, 
                    money.getCoord().y*Place.CELL_SIZE + money.offset);
            
            if (money.showedState == Money.STATE_MUST_REMOVED) {
                this.deleteComponentFromIsland(money);
            }
        }
    }
    
    public GuiJFrame(World world) {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComponent newContentPane = new JPanel();
        
        newContentPane.setOpaque(true); //content panes must be opaque
        this.setContentPane(newContentPane);

        layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(Island.BOARD_SIZE*Place.CELL_SIZE,
                Island.BOARD_SIZE*Place.CELL_SIZE));

        layeredPane.setBorder(BorderFactory.createEmptyBorder());

        for (int i=0; i<Island.BOARD_SIZE; i++) {
            for (int j=0; j<Island.BOARD_SIZE; j++) {
                layeredPane.add(world.island.getPlace(i, j), new Integer(1));
            }
        }

        this.add(layeredPane);
        this.pack();
        this.setVisible(true);
        
        
        //setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    }

    //Make Duke follow the cursor.
   
    
    
}
